<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/3/23
 * Time: 15:47
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\controller\v1;

use app\admin\model\BuddhaCate;
use app\admin\model\BuddhaOrder;
use app\common\lib\Radom;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\lib\Time;
use think\Exception;

class Order extends Base
{
    use paymentApi;

    public function setOrderData()
    {
        $type = $this->request->param('type', 0, 'intval');
        $money = $this->request->param('money', '', 'trim');
        $mark = $this->request->param('mark', '', 'trim');
        $openid = $this->request->param('openid', '', 'trim');
        if ($type == 0) {
            return Show::json_show(Status::mini_error, "请选择捐款类型");
        }
        if (!$money) {
            return Show::json_show(Status::mini_error, "请输入捐款金额");
        }
        if (!empty($mark)) {
            $mark = htmlspecialchars($mark);
        }
        if (empty($openid)) {
            return Show::json_show(Status::mini_error, "用户信息不存在");
        }
        //根据type查询
        $obj = new BuddhaOrder();

        try {
            $type_name = (new BuddhaCate())->where('id', $type)->find();
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "内部错误");
        }
        if (!$type_name) {
            return Show::json_show(Status::mini_error, "类型错误");
        }
        $type_name = $type_name->toArray();
        $order_id = Radom::create_trade_no();
        $data = [
            "cate_id" => $type_name['id'],
            "money" => $money,
            "mark" => $mark,
            "openid" => $openid,
            "order_id" => $order_id
        ];
        $order_pay = [
            "body" => "呷普寺-" . $type_name['title'],
            "out_trade_no" => $order_id,
            "total_fee" => $money * 1000,
            "spbill_create_ip" => $this->request->ip(),
            "notify_url" => (string)url('/notify/pay/notify', [], true, true),
            "trade_type" => "JSAPI",
            "openid" => $openid
        ];
        try {
            $res = $obj->save($data);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "操作失败");
        }
        if ($res) {
            //进行支付
            $res_notify = $this->payment_url->order->unify($order_pay);
            if ($res_notify['return_code'] != "SUCCESS") {
                return Show::json_show(Status::mini_error, "订单错误");
            }
            $result = $this->payment_url->jssdk->bridgeConfig($res_notify['prepay_id'], false);
            return Show::json_show(Status::mini_normal, "ok", $result);
        } else {
            throw new Exception("内部异常");
        }
    }

    public function getOrderByUserList()
    {
        $openid = $this->request->param('openid', "", "trim");
        if (empty($openid)) {
            return Show::json_show(Status::mini_error, "用户信息错误");
        }
        $datetime = $this->request->param('datetime', '', 'trim');
        $day = "本月";
        $where = [];
        if (!empty($datetime)) {
            $y = date('Y', $datetime / 1000);
            $m = date('m', $datetime / 1000);
            $time = Time::startTimeToEndTime($y, $m);
            $where[] = [
                ['o.create_time', "between", $time]
            ];
            if (date('Y-m', $datetime / 1000) != date('Y-m')) {
                $day = date('Y-m', $datetime / 1000) . "月";
            }
        }
//        dump($where);exit();


        try {
            $lists = (new BuddhaOrder())
                ->field("o.id,o.order_id,o.cate_id,o.money,o.create_time,c.id,c.title,o.create_time")
                ->alias('o')
                ->join('buddha_cate c', 'o.cate_id = c.id')
                ->where("openid", $openid)
                ->where($where)
                ->where('payment', 1)->paginate(10)->toArray();
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, "订单错误");
        }
        return Show::json_show(Status::mini_normal, "ok", [
            "row" => $lists,
            "day" => $day
        ]);
    }
}